﻿/*
swfUpload v0.2.5
Copyright (c) 2011 wangli
*/
jQuery.extend({
	files:new Object(),
	createUpload:function(flashID,progressID,src,postUrl,_w,_h){
		var w,h;
		w=(_w)?_w:70;
		h=(_h)?_h:20;
		this.files[flashID]=new upFileList(flashID,progressID,src,postUrl,w,h);
	},
	getFile:function(flashID){
		return this.files[flashID]
	}
}); 

function upFileList(flashID,proID,src,postUrl,_w,_h){
		this.url=postUrl;
		this.postCon={};
		this.domObj();
		this.attr={total:2,size:800,types:"Images (*.jpg, *.gif, *.png):*.jpg;*.gif;*.png"};
		this.pams={flashID:flashID,upUrl:postUrl,id:proID};
		this.fileArr=new Array();
		this.fileNameArr=new Array();
		this.filePathArr=new Array();
		this.upfile=$("#"+proID);
		this.flashID=$(flashID);
		this.file=null;
		this.flash=null;
		var _this=this;
		this.appendFlash(src,flashID,_w,_h,"10.0",this.pams,flashID);
		//this.flash=getFlashMovieObject(flashID);
		var _this=this;
		setTimeout(function (){_this.flash=getFlashMovieObject(flashID);}, 100);
		//alert(this.flash.id);
}
upFileList.prototype.domObj=function(){
	return this.obj;
}
upFileList.prototype.postParameters=function(){
	return this.postCon;
}
upFileList.prototype.postUrl=function(){
	return this.url;
}
upFileList.prototype.getAttr=function(){
	return this.attr;
}
upFileList.prototype.alert=function(val){
	alert(val);
}
//创建每个上传文件
upFileList.prototype.createFile = function (count,fileName) {
	file=new FileProgress("file_"+count,fileName);
	file.count=count;
	file.toggleCancel();
	file.flash=this.flash;
	this.file=file;
  	this.upfile.append(file.content);
	this.fileArr.push(file);
	this.fileNameArr.push(fileName);
	
	var ios=this;
	//覆盖删除文件操作
	file.deleteFile=function(){
		ios.fileArr.splice(this.count,1);
		ios.fileNameArr.splice(this.count,1);
		ios.filePathArr.splice(this.count,1);
		var lg=ios.fileArr.length;
		for(var i=0;i<lg;i++){
			ios.fileArr[i].count=i;
		}
		ios.deleteDataComplete();
	}
};
upFileList.prototype.fileError=function(count,val){
	this.fileArr[count].fileError(val);
	this.filePathArr.push("失败");
}
upFileList.prototype.fileSizeError=function(count,val){
	this.fileArr[count].sizeError(val);
	this.filePathArr.push("失败");
}
upFileList.prototype.fileSetProgress=function(count,val){
	this.fileArr[count].setProgress(parseInt(val));
}
upFileList.prototype.completeHandler=function(count,size,filePath){
	this.fileArr[count].setComplete(size);
	this.filePathArr.push(filePath);
}
//上传完成，由flash组件控制
upFileList.prototype.completeExport=function(){}
//取消并删除文件完成后
upFileList.prototype.deleteDataComplete=function(){}
//设置flash对象
upFileList.prototype.appendFlash=function(scr,conid,w,h,vs,flashvarsObj,id){
	//flashvarsObj传值参数
	var vars="";
	for (var k in flashvarsObj) {
		if (typeof flashvarsObj[k] != "undefined") {
			vars +="&"+ k + "=" + flashvarsObj[k];
		}
		else {
			vars = k + "=" + flashvarsObj[k];
		}
	}
	var fn = function() {         
		var att = { data:scr, width:w, height:h,name:conid};         
		var par = {wmode:"transparent",allowScriptAccess:"always",flashvars:vars};         
		var id = conid;         
		var myObject = swfobject.createSWF(att, par, id);       
	};
	//var myObject = swfobject.createSWF(att, par, id);    
	swfobject.addDomLoadEvent(fn); 
	//swfobject.embedSWF(scr, conid, w, h, vs, null, flashvarsObj, {'quality':'high','wmode':'transparent','allowScriptAccess':'always'},{'id':id,'name':id});
}
//创建每个文件的进度条方法
function FileProgress(ID,con) {
	
	//创建一个加载进度信息容器
	var content = document.createElement("div");
	content.className = "progressWrapper";
	content.id = ID;
  
    //进度条内部容器
	var fileProgressElement = document.createElement("div");
	fileProgressElement.className = "progressContainer";
	//创建取消传送按钮
	var progressCancel = document.createElement("a");
	progressCancel.className = "progressCancel";
	progressCancel.href = "javascript:void(0)";
	progressCancel.appendChild(document.createTextNode("删除"));
	//创建除送内容提示信息
	var progressText = document.createElement("div");
	progressText.className = "progressName";
	progressText.appendChild(document.createTextNode(con));
	//创建加载进度条
	var progressBar = document.createElement("div");
	progressBar.className = "progressBarInProgress";
	//创建加载进度条进度填充
	var progressStatus = document.createElement("div");
	progressStatus.className = "progressBarStatus";
	progressStatus.innerHTML = "&nbsp;";
	progressBar.appendChild(progressStatus);
	//将内容个添加到容器
	fileProgressElement.appendChild(progressText);
	fileProgressElement.appendChild(progressBar);
	fileProgressElement.appendChild(progressCancel);
	content.appendChild(fileProgressElement);
	
	this.flash=null;
	this.count=0;
	this.content=content;
	this.conID=ID;
	this.fileProgressElement=fileProgressElement;
	this.progressCancel=progressCancel;
	this.progressText=progressText;
	this.progressBar=progressBar;
	this.progressStatus=progressStatus;
}
//文件传输错误
FileProgress.prototype.fileError = function (val) {
	this.fileProgressElement.className = "progressContainer red";
	$(this.progressText).append("&nbsp;&nbsp;"+val);
	$(this.progressBar).remove();
};
//文件大小不符合
FileProgress.prototype.sizeError = function (bytesTotal) {
	this.fileProgressElement.className = "progressContainer red";
	$(this.progressText).append("&nbsp;&nbsp;("+bytesTotal+"KB&nbsp;) 文件太大了");
	$(this.progressBar).remove();
};
//取消上传
FileProgress.prototype.toggleCancel = function () {
	//this.progressCancel.style.visibility = show ? "visible" : "hidden";
	var ios=this;
	$(this.progressCancel).click(function(){
		//删除html中的上传进度条
		$(ios.content).remove();
		//停止并删除flash组件中的正在上传中的文件
		ios.flash.deleteFile(ios.count);
		//删除执行文件后更改数据
		ios.deleteFile();
		//删除服务器上的文件
		ios.deleteServerFile();
	}); 
};
//删除自身文件操作
FileProgress.prototype.deleteFile=function(){}
//删除服务器上的文件
FileProgress.prototype.deleteServerFile=function(){}
//上传进度样式更改
FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.progressStatus.style.width = percentage + "%";
};
//上传完成之后更改状态
FileProgress.prototype.setComplete = function (bytesTotal) {
	$(this.progressBar).remove();
	$(this.progressText).append("&nbsp;&nbsp;("+bytesTotal+"KB&nbsp;)");
	//$(this.progressText).append("&nbsp;&nbsp;"+bytesTotal+"KB&nbsp;");
};
//获取flash文件对象
function getFlashMovieObject(movieName)
{
	
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else
  {
    return document.getElementById(movieName);
  }
}
//常用上传类型简易封装1.0
var upAuto=new Array();
function createUpObj(UPLoad,swf,postUrl){
	this.newup=new createUpElement(UPLoad,UPLoad,UPLoad,swf,postUrl);
	upAuto.push(this.newup);
	document.write(this.newup.elementDiv);
}
createUpObj.prototype.attr=function(val){
	this.newup.attr=val;
}
createUpObj.prototype.postCon=function(val){
	this.newup.postCon=val;
}
createUpObj.prototype.upType=function(val){
	alert(val);
	this.newup.upType=val;
}
//创建固定元素
function createUpElement(UPLoad,upData,upLoadItem,swf,postUrl){
	this.UPLoad="flash_"+UPLoad;
	this.upData=upData;
	this.upLoadItem="upLoadItem_"+upLoadItem;
	this.swf=swf;
	this.postUrl=postUrl;
	this.attr=null;
	this.postCon=null;
	this.upType="image";
	this.elementDiv="<div id=\""+this.UPLoad+"\"></div><input name=\""+this.upData+"\" type=\"hidden\" value=\"\" id=\""+this.upData+"\"/><div id=\""+this.upLoadItem+"\" class=\"uploaditem\"></div>";
}
jQuery(function($){
	if(upAuto.length>0){
		for(var i=0;i<upAuto.length;i++){
			$.createUpload(upAuto[i].UPLoad,upAuto[i].upLoadItem,upAuto[i].swf,upAuto[i].postUrl);
			var upfile=$.getFile(upAuto[i].UPLoad);
			upfile.upInput=upAuto[i].upData;
			//附加post传送其他参数
			if(upAuto[i].postCon!=null){
				upfile.postCon=upAuto[i].postCon;
			}else{
				upfile.postCon={path:""};
			}
			//附加上传文件类型
			if(upAuto[i].attr!=null){
				upfile.attr=upAuto[i].attr
			}else{
				switch(upAuto[i].upType){
					case "image":
						upfile.attr={total:5,size:1024,types:"图片(*.jpg, *.gif, *.png):*.png;*.gif;*.jpg;*.bmp;*.jpeg;*.PNG;*.GIF;*.JPG;*.BMP;*.JPEG"};
						break;
					case "file":
						upfile.attr={total:5,size:61440,types:"文件(*.doc, *.pdf, *.rar, *.zip,*.xls,*doc):*.doc;*.pdf;*.rar;*.zip;*.xls;*.txt;*.docx;*.xlsx;*.DOC;*.PDF;*.RAR;*.ZIP;*.XLS;*.TXT;*.DOCX;*.XLSX"};
						break;
					default :
						upfile.attr={total:5,size:1024,types:"图片(*.jpg, *.gif, *.png):*.png;*.gif;*.jpg;*.bmp;*.jpeg;*.PNG;*.GIF;*.JPG;*.BMP;*.JPEG"};
				}
				
			}			
			upfile.completeExport=function(){
				$("#"+this.upInput).val(this.filePathArr);
			}
			upfile.deleteDataComplete=function(){
				$("#"+this.upInput).val(this.filePathArr);
			}
		}
	}
})
